//
//  EFASettingVC.m
//  efairy
//
//  Created by XiangDongMBP on 2017/7/12.
//  Copyright © 2017年 East. All rights reserved.
//

#import "EFASettingVC.h"
#import "EFAMoreSetTCell.h"
#import "EFALoginVC.h"
#import "GlobalHeader.h"
#import <RongIMLib/RongIMLib.h>

@interface EFASettingVC ()<UITableViewDelegate, UITableViewDataSource, EFAMoreSetTCellDelegate>

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet UIButton *logoutBtn;
@property (strong, nonatomic) NSArray *dataArray;

@end

static NSString *const kReuseCellId = @"EFAMoreSetTCell";

@implementation EFASettingVC

#pragma mark - life cycle
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self initNavigationBar];
    [self initSubViews];
}

- (BOOL)hidesBottomBarWhenPushed {
    return YES;
}

- (void)initNavigationBar {
    self.navigationItem.title = @"设置";
    self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:nil action:nil];
}

- (void)initSubViews {
    self.logoutBtn.layer.masksToBounds = YES;
    self.logoutBtn.layer.cornerRadius = 4;
    [self.logoutBtn addTarget:self action:@selector(logoutBtnTapped:) forControlEvents:UIControlEventTouchUpInside];
    
    UINib *nib = [UINib nibWithNibName:kReuseCellId bundle:nil];
    [self.tableView registerNib:nib forCellReuseIdentifier:kReuseCellId];
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    self.tableView.estimatedRowHeight = 67.0;
    //    self.tableView.tableFooterView = [UIView new];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.dataArray.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    EFAMoreSetTCell *cell = [tableView dequeueReusableCellWithIdentifier:kReuseCellId forIndexPath:indexPath];
    if(indexPath.section < self.dataArray.count) {
        NSDictionary *dic = self.dataArray[indexPath.section];
        cell.leftTitleLabel.text = dic[@"title"];
        cell.rightDetailLabel.text = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
        cell.uiswitch.hidden = ![dic[@"title"] isEqualToString:@"设备新消息提醒"];
        NSString *appName = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleDisplayName"];
        cell.rightDetailLabel.hidden = ![dic[@"title"] isEqualToString:appName];
        [cell.uiswitch setOn:YES animated:YES];
        // 消息提醒开关
//        [[RCIMClient sharedRCIMClient] getNotificationQuietHours:^(NSString *startTime, int spansMin) {
//            dispatch_async(dispatch_get_main_queue(), ^{
//                [cell.uiswitch setOn:!spansMin animated:YES];
//            });
//        } error:nil];
    }
    cell.delegate = self;
    return cell;
}

#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 15.0;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return (section == 1) ? 45.0 : 0.1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section < self.dataArray.count) {
        NSDictionary *dic = self.dataArray[indexPath.section];
        return [dic[@"rowHeight"] floatValue];
    }
    return 45.0;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
//    if (indexPath.section == 0) {   //!< 个人资料
//        [self jumpToPersonalInfo];
//    }
}

#pragma mark - event response
- (void)logoutBtnTapped:(UIButton *)btn {
    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"确定退出登录？" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    [alertVC addAction:[UIAlertAction actionWithTitle:@"退出登录" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        [[NSNotificationCenter defaultCenter] postNotificationName:EFAGlobalNotiLogout object:nil];
    }]];
    [alertVC addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:nil]];
    [self presentViewController:alertVC animated:YES completion:nil];
}

#pragma mark - EFAMoreSetTCellDelegate
- (void)moreSetTCell:(EFAMoreSetTCell *)cell uiSwitchChanged:(UISwitch *)uiSwitch {
    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
    if (indexPath.section < self.dataArray.count) {
        NSDictionary *dic = self.dataArray[indexPath.section];
        if ([dic[@"title"] isEqualToString:@"设备新消息提醒"]) {
            if (uiSwitch.isOn) {
//                [[RCIMClient sharedRCIMClient] removeNotificationQuietHours:nil error:^(RCErrorCode status) {
//                    dispatch_async(dispatch_get_main_queue(), ^{
//                        [uiSwitch setOn:NO animated:YES];
//                    });
//                }];
                [uiSwitch setOn:NO animated:YES];
            } else {
//                [[RCIMClient sharedRCIMClient] setNotificationQuietHours:@"00:00:00" spanMins:1439 success:nil error:^(RCErrorCode status) {
//                    dispatch_async(dispatch_get_main_queue(), ^{
//                        [uiSwitch setOn:YES animated:YES];
//                    });
//                }];
                 [uiSwitch setOn:YES animated:YES];
            }
        }
    }
}

#pragma mark - private methods
- (void)jumpToPersonalInfo {
    ;
}
//- (void)jumpToLoginVC {
//    UINavigationController *navLogin = [[UINavigationController alloc] initWithRootViewController:[[EFALoginVC alloc] init]];
//    [UIApplication sharedApplication].keyWindow.rootViewController = navLogin;
//    
//    //转场动画
//    CATransition *anim = [CATransition animation];
//    //    anim.type = @"rippleEffect";
//    anim.type = @"moveIn";
//    anim.subtype = @"fromRight";
//    anim.duration = 0.5;
//    [[UIApplication sharedApplication].keyWindow.layer addAnimation:anim forKey:nil];
//}
#pragma mark - network request
#pragma mark - getters and setters
- (NSArray *)dataArray {
    if (!_dataArray) {
        NSString *appName = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleDisplayName"];
        _dataArray = @[@{@"title" : appName,
                         @"rowHeight" : @(45)},
                       @{@"title" : @"设备新消息提醒",
                         @"rowHeight" : @(45)}];
    }
    return _dataArray;
}

@end
